interface_exists
检查接口是否已定义
适用PHP版本:PHP 4 和更高版本
interface_exists 函数用来检测某个接口是否已经定义。
interface_exists(string $interface, bool $autoload = true): bool
如果指定的接口已经定义,返回 true;否则返回 false。
<?php interface MyInterface {} <p>if (interface_exists('MyInterface')) {<br> echo '接口 MyInterface 已定义';<br> } else {<br> echo '接口 MyInterface 未定义';<br> }<br> ?><br>
在这个示例中,首先定义了一个接口 MyInterface,然后使用 interface_exists 函数来检查该接口是否已经定义。如果接口已定义,输出“接口 MyInterface 已定义”;否则输出“接口 MyInterface 未定义”。